package com.dgshanger.sanhxiaofeisc.mvp.presenter

import android.text.TextUtils
import com.blankj.utilcode.util.RegexUtils
import com.dgshanger.sanhxiaofeisc.entity.ShopEnterEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.ShopEnterContract
import com.dgshanger.sanhxiaofeisc.mvp.model.ShopEnterModel
import com.xx.baseuilibrary.mvp.BaseMvpPresenter
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import java.io.File

/**
 * Created by JacobHHH on 2018/4/20.
 */
class ShopEnterPresenter : BaseMvpPresenter<ShopEnterContract.Model, ShopEnterContract.View>(), ShopEnterContract.Presenter {

    override fun checkParameter(fileList: Map<String,File>?, map: Map<String, String>?) {
        if (TextUtils.isEmpty(map!!["shop_company_name"])){
            getView()?.showToast("请输入单位全称")
            return
        }else if (TextUtils.isEmpty(map!!["shop_company_address"])){
            getView()?.showToast("请输入单位地址")
            return
        }else if (TextUtils.isEmpty(map!!["shop_company_email"])){
            getView()?.showToast("请输入邮箱地址")
            return
        }else if(!RegexUtils.isEmail(map!!["shop_company_email"])){
            getView()?.showToast("请输入正确的邮箱地址")
            return
        }else if (TextUtils.isEmpty(map!!["shop_company_contract"])){
            getView()?.showToast("请输入联系电话")
            return
        }else if (map!!["shop_company_contract"]!!.length!=11){
            getView()?.showToast("请输入正确的手机号")
            return
        }else if (TextUtils.isEmpty(map!!["shop_company_corporate"])){
            getView()?.showToast("请输入法人名字")
            return
        }else if (TextUtils.isEmpty(map!!["shop_company_corporate_id"])){
            getView()?.showToast("请输入法人身份证号码")
            return
        }else if (!RegexUtils.isIDCard18(map!!["shop_company_corporate_id"])){
            getView()?.showToast("请输入正确的身份证号码")
            return
        }else if (fileList!!["file0"]==null){
            getView()?.showToast("请添加营业执照")
            return
        }else if (fileList!!["file1"]==null){
            getView()?.showToast("请添加身份证正面照")
            return
        }else if (fileList!!["file2"]==null){
            getView()?.showToast("请添加身份证反面照")
            return
        }
        getView()?.checkSuccess()
    }

    override fun submitAudit(fileList: Map<String,File>?, map: Map<String, String>?) {
        getModel().submitAudit(fileList,map,object : XxBaseHttpObserver<ShopEnterEntity>(){
            override fun onCompleted(msg: String?, entity: ShopEnterEntity?) {
                getView()?.submitAuditSuccess(entity!!.apply_sn)
            }

            override fun onError(error: String?) {
                getView()?.dismissLoadingDialog()
                getView()?.showToast(error)
            }

            override fun onStart() {
                getView()?.showLoadingDialog()
            }

            override fun onFinish() {
                getView()?.dismissLoadingDialog()
            }

        })

    }

    override fun createModel(): ShopEnterContract.Model = ShopEnterModel()
}